﻿using BigEgg.Framework.Applications.ViewModels;
using FMStudio.Templates.DesignTool.Application.Models;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Windows.Input;

namespace FMStudio.Templates.DesignTool.Application.Services
{
    [Export(typeof(IToolboxService)), Export]
    public class ToolboxService : DataModel, IToolboxService
    {
        #region Fields
        private readonly ObservableCollection<ToolboxItem> items;
        private readonly ReadOnlyObservableCollection<ToolboxItem> readOnlyItems;
        private ToolboxItem selectedItem;

        private ICommand addItemCommand;
        #endregion

        [ImportingConstructor]
        public ToolboxService()
        {
            this.items = new ObservableCollection<ToolboxItem>();
            this.readOnlyItems = new ReadOnlyObservableCollection<ToolboxItem>(this.items);
        }

        #region Properties
        public ReadOnlyObservableCollection<ToolboxItem> Items { get { return this.readOnlyItems; } }

        public ToolboxItem SelectedItem
        {
            get { return this.selectedItem; }
            set
            {
                if (this.selectedItem != value)
                {
                    this.selectedItem = value;
                    RaisePropertyChanged("SelectedItem");
                }
            }
        }

        public ICommand AddItemCommand
        {
            get { return this.addItemCommand; }
            set
            {
                if (this.addItemCommand != value)
                {
                    this.addItemCommand = value;
                    RaisePropertyChanged("AddItemCommand");
                }
            }
        }
        #endregion

        #region Methods
        #region Public Methods
        public void AddItem(ToolboxItem item)
        {
            if (item == null) { throw new ArgumentException("item"); }
            this.items.Add(item);
        }

        public void RemoveItem(ToolboxItem item)
        {
            if (item == null) { throw new ArgumentException("item"); }
            if (!this.items.Contains(item)) { throw new ArgumentException("item"); }
            this.items.Remove(item);
        }
        #endregion
        #endregion
    }
}
